package com.itheima.restkeeper.face;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.restkeeper.StoreFace;
import com.itheima.restkeeper.pojo.Store;
import com.itheima.restkeeper.req.StoreVo;
import com.itheima.restkeeper.service.IStoreService;
import com.itheima.restkeeper.utils.BeanConv;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.config.annotation.Method;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @ClassName StoreFaceImpl.java
 * @Description 门店dubbbo服务实现
 */
@Slf4j
@DubboService(version = "${dubbo.application.version}",timeout = 5000,
    methods ={
        @Method(name = "findStoreVoPage",retries = 2),
        @Method(name = "createStore",retries = 0),
        @Method(name = "updateStore",retries = 0),
        @Method(name = "deleteStore",retries = 0)
    })
public class StoreFaceImpl implements StoreFace {

    @Autowired
    IStoreService storeService;

    /**
     * 分页查询门店
     * @param storeVo 查询条件
     * @param pageNum 页码
     * @param pageSize 每页条数
     * @return
     */
    @Override
    public Page<StoreVo> findStoreVoPage(StoreVo storeVo, int pageNum, int pageSize) {
        //查询Page<Store>门店分页
        Page<Store> storePage = storeService.findStoreVoPage(storeVo, pageNum, pageSize);
        //转化Page<Store>为Page<StoreVo>
        Page<StoreVo> storeVoPage= new Page<>();
        BeanConv.toBean(storePage, storeVoPage);
        //转换List<Store>为 List<StoreVo>
        List<Store> records = storePage.getRecords();
        storeVoPage.setRecords(BeanConv.toBeanList(records,StoreVo.class));
        //返回结果
        return storeVoPage;
    }

    /**
     * 新增门店
     * @param storeVo 对象信息
     * @return
     */
    @Override
    public StoreVo createStore(StoreVo storeVo) {
        Store store = storeService.createStore(storeVo);
        return BeanConv.toBean(store,StoreVo.class);
    }

    /**
     * 修改门店
     * @param storeVo 对象信息
     * @return
     */
    @Override
    public Boolean updateStore(StoreVo storeVo) {
        return  storeService.updateStore(storeVo);
    }

    /**
     * 根据点选id删除门店
     * @param checkedIds 选择对象信息Id
     * @return
     */
    @Override
    public Boolean deleteStore(String[] checkedIds) {
        return storeService.deleteStore(checkedIds);
    }

    /**
     * 根据门店id查询门店
     * @param storeId 选择对象信息Id
     * @return
     */
    @Override
    public StoreVo findStoreByStoreId(Long storeId) {
        Store byId = storeService.getById(storeId);
        return BeanConv.toBean(storeId,StoreVo.class);
    }

    /**
     * 查询门店集合
     * @return
     */
    @Override
    public List<StoreVo> findStoreVoList() {
        return BeanConv.toBeanList(storeService.findStoreVoList(),StoreVo.class);
    }
}
